package customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.zhny.wxginger.R;

/**
 * Created by Alex on 2017/1/17.
 */

public class LineChartView extends View {
    private int Xpoint;
    private int Ypoint;
    private int Xlength;
    private int Ylength;
    private int Xscale;
    private int Yscale;
    private int labelTextSize;
    private String title;
    private String unit;
    private int Ynumber;
    private int YMaxValue;
    private int YMinValue;
    private double scaleValue;

    private final int TextSize;
    private final int Background;


    private Paint textPaint;//文本画笔
    private Paint paint;//坐标轴画笔
    private Paint strokePaint;//直线画笔


    private String[] XLabel = {"aa", "bb", "cc", "dd"};  // time
    private double[] YLabel = {15, 20, 15, 30}; // data

    //通过view的size计算数据

    //接收数据
    public void setData(String[] time, double[] data, String title, String unit) {
        this.XLabel = time;
        this.YLabel = data;
        this.title = title;
        this.unit = unit;
    }

    public LineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LineChartView);
        Ynumber = typedArray.getInt(R.styleable.LineChartView_Ynumber, 0);
        TextSize = (int) typedArray.getDimension(R.styleable.LineChartView_TextSize, 0);
        labelTextSize = (int) typedArray.getDimension(R.styleable.LineChartView_labelTextSize, 0);
        YMaxValue = typedArray.getInt(R.styleable.LineChartView_YMaxValue, 0);
        YMinValue = typedArray.getInt(R.styleable.LineChartView_YMinValue, 0);
        Background = typedArray.getColor(R.styleable.LineChartView_Background, 0);
        scaleValue = (double) (YMaxValue - YMinValue) / Ynumber;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Background);
        // 获取view的宽度和长度
        final int viewWidth = getWidth();
        final int viewHeight = getHeight();

        //原点位置
        Xpoint = (int) (viewWidth * 0.09); //数值越大越靠右
        Ypoint = (int) (viewHeight  - 3 * labelTextSize); // 数值越大越靠上

        //X,Y轴长度 数值越大越长
        Xlength = (int) (viewWidth * 0.92); // 真实的长度为Xlength - Xpoint
        Ylength = (int) (viewHeight * 0.85);

        //指定刻度
        Xscale = (Xlength - Xpoint - 30) / XLabel.length;
        Yscale = (Ylength - 30) / Ynumber;

        //定义画笔
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(2);
        paint.setTextSize(labelTextSize);

        strokePaint = new Paint();
        strokePaint.setAntiAlias(true);
        strokePaint.setColor(Color.BLACK);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(4);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(2);
        textPaint.setTextSize(TextSize);

        //画X,Y轴
        //X
        canvas.drawLine(Xpoint, Ypoint, Xlength, Ypoint, paint);
        //Y
        canvas.drawLine(Xpoint, Ypoint, Xpoint, Ypoint - Ylength, paint);

        //绘制折线
        //刻度&标示
        //X
        Path path = new Path();
        Path textpath;
        int Xposi = Xscale + Xpoint;
        int Yposi = Ypoint - Yscale;
        float lt = labelTextSize / 2;
        float ylt = Ypoint + labelTextSize;
        float yltt = (float) (ylt + 1.7 * labelTextSize);
        for (int i = 0; i < XLabel.length; i++) {
            textpath = new Path(); //文本路径
            canvas.drawLine(Xposi, Ypoint, Xposi, Ypoint - 7, paint);
            textpath.moveTo((float) (Xposi - labelTextSize * 1.7), yltt);
            textpath.lineTo(Xposi, Ypoint);
            canvas.drawTextOnPath(XLabel[i], textpath, 0, lt, paint);

            //画折线
            if (i == 0)
                path.moveTo(Xposi, (float) (Ypoint - (YLabel[i] - YMinValue) / scaleValue * Yscale));
            else
                path.lineTo(Xposi, (float) (Ypoint - (YLabel[i] - YMinValue) / scaleValue * Yscale));
            canvas.drawCircle(Xposi, (float) (Ypoint - (YLabel[i] - YMinValue) / scaleValue * Yscale), 6, paint);
            Xposi += Xscale;
            Yposi -= Yscale;
        }
        canvas.drawPath(path, strokePaint);


        //Y
        paint.setTextAlign(Paint.Align.RIGHT);
        Yposi = Ypoint;
        float xlt = Xpoint - labelTextSize / 2;
        float x3 = labelTextSize / 3;
        for (int i = 0; i <= Ynumber; i++) {
            canvas.drawLine(Xpoint, Yposi, Xpoint + 7, Yposi, paint);
            canvas.drawText((int) (YMinValue + scaleValue * i) + "", xlt
                    , Yposi + x3, paint);
            Yposi -= Yscale;
        }

        drawText(YLabel, canvas, Xscale + Xpoint);

        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(TextSize + 17);
        canvas.drawText(title + " " + unit, Xlength / 2 + TextSize, Ypoint - Ylength + TextSize, textPaint);
    }

    // 绘制文本
    public void drawText(double[] YLabel, Canvas canvas, int Xposi) {
        int flag = 0;
        for (int i = 0; i < YLabel.length; i++) {
            if (i == 0 && YLabel[i] < YLabel[i + 1] || i == YLabel.length - 1 && YLabel[i] < YLabel[i - 1])
                flag = 1;
            else if (i == 0 && (YLabel[i] > YLabel[i + 1]||YLabel[i] == YLabel[i + 1]) || i == YLabel.length - 1 && (YLabel[i] > YLabel[i - 1] || YLabel[i] == YLabel[i - 1]))
                flag = 2;
            else if (YLabel[i] < YLabel[i + 1] && YLabel[i] < YLabel[i - 1])  //极小值点
                flag = 1;
            else if (YLabel[i] > YLabel[i + 1] && YLabel[i] > YLabel[i - 1])  //极大值点
                flag = 2;
            else
                flag = 0;
            switch (flag) {
                case 1:
                    canvas.drawText(new Integer((int) YLabel[i]).toString(), Xposi, (float) (Ypoint - (YLabel[i] - YMinValue) / scaleValue * Yscale + TextSize * 1.2), textPaint);
                    break;
                case 2:
                    canvas.drawText(new Integer((int) YLabel[i]).toString(), Xposi, (float) (Ypoint - (YLabel[i] - YMinValue) / scaleValue * Yscale - TextSize * 0.5), textPaint);
                    break;
                default:
                    canvas.drawText(new Integer((int) YLabel[i]).toString(), Xposi, (float) (Ypoint - (YLabel[i] - YMinValue) / scaleValue * Yscale - TextSize * 0.7), textPaint);
                    break;
            }
            Xposi += Xscale;
        }
    }

    public void setYMaxValue(int YMaxValue) {
        this.YMaxValue = YMaxValue;
        scaleValue = (double) (YMaxValue - YMinValue) / Ynumber;
    }

    public void setYMinValue(int YMinValue) {
        this.YMinValue = YMinValue;
        scaleValue = (double) (YMaxValue - YMinValue) / Ynumber;
    }

    public void setYnumber(int ynumber) {
        Ynumber = ynumber;
        scaleValue = (double) (YMaxValue - YMinValue) / Ynumber;
        Yscale = (Ylength - 30) / Ynumber;
    }

    public void setYMin_MaxValue() {
        System.out.println(YLabel.length +"****************** YLabel.length in setYMin_MaxValue");
        double max = YLabel[0];
        double min = YLabel[0];
        for (int i = 0; i < YLabel.length; i++) {
            if (max < YLabel[i]) {
                max = YLabel[i];
            }
            if (min > YLabel[i]) {
                min = YLabel[i];
            }
            System.out.println(YLabel[i] + "  " +i);
        }

        System.out.println(max);
        System.out.println(min);
        this.YMaxValue = (int)max+2;
        this.YMinValue = (int)min-2;
        scaleValue = (double) (YMaxValue - YMinValue) / Ynumber;
    }
}